<?php
include_once(SERVER_ROOT_PATH.'/include/config/ml_3rdType_config.php');
class ml_biz_extRelation_extFollow {
    
    private $rds;
    private $openapis;
    private $thisapi;
    
    function __construct(){
        $this->rds = new ml_model_rdsExtFollow();
        $this->openapis = array();
        $this->thisapi = null;
    }
    
    public function get_meilauser_3rdfollow($meilauid){
        
        $num = $this->rds->get_extFollow_count($meilauid);
        if (empty($num)) {
            return false;
        }
        $re = $this->rds->get_extFollow($meilauid, 0, 6);
        $oNick=new ml_model_rdsHash();
        foreach ($re as $key => $value) {
            if ($key < 4) {
                $data['friend'][$key]['nickname']= Tool_string::un_html($oNick->getNickByUid($value));
                $data['friend'][$key]['head_url']= ml_tool_picid::uid2portrait($value);
                $data['friend'][$key]['uid']= $value;
            } else {
                $data['other'][] = Tool_string::un_html($oNick->getNickByUid($value));
            }
            
        }

        $tmp = count($data['other']);
        
        if ( $tmp == 3) {
            $data['other'] = implode('、', $data['other']);
            $data['other'] .= "等";
            $data['num2'] = $num-4;
        } else {
            $data['other'] = implode('、', $data['other']);
            $data['num2'] = $tmp;
        }
                
        $data['num'] = $num;
        return $data;
    }
    
    public function get_wb3rdfollow($trd_uid, $page = 1, $num = 4){
        if (empty($this->openapis[TRD_TYPE_ID_SINA])) {
                    $this->openapis[TRD_TYPE_ID_SINA]= new ml_model_openapiWeibo ();
                    $this->thisapi = $this->openapis[TRD_TYPE_ID_SINA];
        }
        
        $data['access_token'] = ML_WEIBO_ACCESS_TOKEN;
        
        $data['3rd_id'] = $trd_uid;
        $back = array();
        $userfollows = $this->thisapi->get_user_follows($data);
        shuffle($userfollows['users']);
        foreach ($userfollows['users'] as $key => $value ){
            if ($value['gender'] == 'f') {
                $re = $this->rds->get_meilauid_by_3rdid($value['id'], 1);
                if (empty($re)){
                $back['users'][] = $value;
                if (count($back['users']) == $num) {
                    break;
                }
                }
            }
        }
        return $back;
        
    }
    
    //
    public function get_meilauser_3rdfollowPage($meilauid , $page= 1, $num = 16){
        
        $count = $this->rds->get_extFollow_count($meilauid);

        $start = ($page-1)*$num;
        $end = $start+$num-1;
        
        $data1 = $this->rds->get_extFollow($meilauid, $start, $end);
        $data2 = $this->rds->get_my3rdFriend($meilauid, TRD_TYPE_ID_SINA);
        rsort($data2,SORT_NUMERIC );
        $count2 = count($data2);
        
        if ($count <= $start) {
            $data2 = array_slice($data2, $start-$count, $num);   
        } elseif ($count > $end) {
            $data2 = array();
        }else {
            $data2 = array_slice($data2, 0, $end-$count+1);
        }
        
        $oNick=new ml_model_rdsHash();
        foreach ($data1 as $key => $value) {
                $bdata1['friend'][$key]['nickname']= Tool_string::un_html($oNick->getNickByUid($value));
                $bdata1['friend'][$key]['head_url']= ml_tool_picid::uid2portrait($value);
                $bdata1['friend'][$key]['uid']= $value;
                $bdata1['friend'][$key]['isfollowed'] = false;            
        }
        
        foreach ($data2 as $key => $value) {
                $bdata2['friend'][$key]['nickname']= Tool_string::un_html($oNick->getNickByUid($value));
                $bdata2['friend'][$key]['head_url']= ml_tool_picid::uid2portrait($value);
                $bdata2['friend'][$key]['uid']= $value;
                $bdata2['friend'][$key]['isfollowed'] = true;            
        }
        
        $bdata['friend'] = array_merge((array)$bdata1['friend'],(array)$bdata2['friend']);
        $bdata['total_number'] = $count+$count2;
        
        return $bdata;
    }
    
    public function get_meilauser_3rdfollowedPage($meilauid) {
        
        $re = $this->rds->get_my3rdDriend($meilauid, TRD_TYPE_ID_SINA);
    }
    
    
    public function init_newExtFollow_redis($meila_uid, $trd_uid, $type_id){
        switch ($type_id){
            case TRD_TYPE_ID_SINA :
                
                $re = $this->get_meilauser_3rdfollow($meila_uid);
                if (!empty($re)) return ;
                
                
                if (empty($this->openapis[TRD_TYPE_ID_SINA])) {
                    $this->openapis[TRD_TYPE_ID_SINA]= new ml_model_openapiWeibo ();
                    $this->thisapi = $this->openapis[TRD_TYPE_ID_SINA];
                }
                    
                $data['access_token'] = ML_WEIBO_ACCESS_TOKEN;
                
                $data['3rd_id'] = $trd_uid;
                    
                $userfollows = $this->thisapi->get_user_follows_ids($data);
                if (!$userfollows) {
                    $data['access_token'] = ML_WEIBO_ACCESS_TOKEN;
                    $userfollows = $this->thisapi->get_user_follows_ids($data);
                    $userfans = $this->thisapi->get_user_fans_ids($data);
                }
                break;
                //var_dump($userfans);
                //var_dump($userfollows);
                            
            default:
                    return false;
        }
        
        $this->rds->push_new_3rd($meila_uid, $type_id);
        $this->rds->init_3rd_to_meilauid($trd_uid, $meila_uid, $type_id);
        
        $a = new ml_model_atNick_rdsFollowList();
        
        foreach ($userfollows['ids'] as $key => $value){
            $re = $this->rds->get_meilauid_by_3rdid($value, $type_id);
            if ($re) {
                //list
                $b = $a->isMemberFollowList($meila_uid, $re);
                if (!$b){
                $this->rds->push_new_extFollow($meila_uid, $re, $type_id);
                }
                }
            }
                
        foreach ($userfans['ids'] as $key => $value){
            $re = $this->rds->get_meilauid_by_3rdid($value, $type_id);
            if ($re) {
                //list
            $b = $a->isMemberFollowList($re, $meila_uid);
                if (!$b){
                $this->rds->push_new_extFollow($re, $meila_uid, $type_id);
                }
                
            }
        }
    }
    
    
}
    